package com.whut.manage.controller;
import com.alibaba.fastjson.JSONObject;
import com.whut.manage.pojo.HistoryData;
import com.whut.manage.service.HistoryDataAnalysisService;
import com.whut.manage.service.HistoryDataQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @author WZH
 * @create 2021-03-09 11:32
 * @desc 查询历史数据
 **/
@Slf4j
@RestController
@CrossOrigin(origins="*",maxAge=3600)
public class HistoryDataController {
    @Autowired
    private HistoryDataQueryService historyDataQueryService;

    @GetMapping("/query")
    public Object hello(HttpServletRequest request) {
        //从request域中取出来相应的参数
        String metric = request.getParameter("metric");
        String s_startTime = request.getParameter("startTime");
        String s_endTime = request.getParameter("endTime");
        String deviceName=request.getParameter("deviceName");
        //将字符串的时间格式转换成Date对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;
        try {
            startTime = simpleDateFormat.parse(s_startTime);
            endTime = simpleDateFormat.parse(s_endTime);
        } catch (ParseException e) {
            log.error(e.getMessage());
        }
//      将查询结果以json字符串形式返回
        return historyDataQueryService.findByMetric(metric,startTime,endTime,deviceName);
    }


    /**
     *根据metric[]数据来查询历史数据
     * @return {{@link Object}}
     * @author WZH
     * @date 2021/3/12 11:16
     */
    @PostMapping("api/query")
    public Object metricsQuery(@RequestBody HistoryData historyData) {
        List<String> metric = historyData.getMetric();
        String deviceName=historyData.getDeviceName();
        //创建一个时间格式化对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime_date = null;
        Date endTime_date = null;
        try {
            startTime_date = simpleDateFormat.parse(historyData.getStartTime());
            endTime_date = simpleDateFormat.parse(historyData.getEndTime());
        } catch (ParseException e) {
            log.error(e.getMessage());
        }
//      将查询结果以json字符串形式返回

        return historyDataQueryService.findByMetrics(metric, startTime_date, endTime_date,deviceName);
    }

    /**
     *
     * @param historyData
     * @return {{@link JSONObject}}
     * @author WZH
     * @date 2021/6/9 17:03
     */
    @PostMapping("api/analysis")
    public JSONObject histroyDataAnalysis(@RequestBody HistoryData historyData){
        System.out.println(historyData.getStartTime());
        //创建一个时间格式化对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime_date = null;
        Date endTime_date = null;
        try {
            startTime_date = simpleDateFormat.parse(historyData.getStartTime());
            endTime_date = simpleDateFormat.parse(historyData.getEndTime());
        } catch (ParseException e) {
            log.error(e.getMessage());
        }

//      将查询结果以json字符串形式返回
        return HistoryDataAnalysisService.histroyProducitonDataAnalysis(startTime_date, endTime_date,historyData.getDeviceName());
    }

//    @RequestMapping("/hello")
//    public String hello(){
//        return "hello";
//    }
}
